<?php

class Vehicle_insurance extends CActiveRecord
{
	/**
	 * The followings are the available columns in table 'Vehicle_insurance':
	 * @var integer $id
	 * @var integer $fk_truck_id
	 * @var string $premium_name
	 * @var string $insurer
	 * @var string $policyno
	 * @var double $premium
	 * @var string $effectdate
	 * @var string $lapsedate
	 * @var integer $kind
	 * @var integer $state
	 * @var string $create_time
	 */
	
	public $property_owner;
	public $plate_no;
	public $type;
	private $_kind=array('0'=>'车辆','1'=>'尾板');
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'vehicle_insurance';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('premium_name','length','max'=>30),
			array('insurer','length','max'=>100),
			array('policyno','length','max'=>50),
			array('premium', 'numerical'),
			array('property_owner,plate_no,type,premium_name,insurer,policyno,premium,insurance_cost,effectdate,lapsedate,another_cost,total_cost', 'required'),
			array('premium,insurance_cost,another_cost,total_cost', 'match','pattern' => '/^\d{1,16}(\.\d{1,2})?$/','message'=>'{attribute} 整数位最多16位，小数位2位'),
			array('effectdate,lapsedate', 'type','type'=>'date','message'=>'{attribute} 必须为日期型(年-月-日)'),
		);
	}

	/**
	 * Prepares attributes before performing validation.
	 */
	protected function beforeValidate($on)
	{
		if($this->isNewRecord){
			$this->state=0;
			$this->create_time=date('Y-m-d G:i:s');
		}
		return true;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => '索引号',
			'property_owner' => '产权单位',
			'plate_no' => '车辆/尾板牌号',
			'type' => '车型',
			'fk_truck_id' => '车辆id',
			'premium_name' => '险种名称',
			'insurer' => '承保公司',
			'policyno' => '保单号',
			'premium' => '投保金额(元)',
			'insurance_cost' => '保险费用(元)',
			'another_cost' => '其他费用(元)',
			'total_cost' => '总费用(元)',
			'effectdate' => '保险生效时间',
			'lapsedate' => '保险终止时间',
			'kind' => '车辆/尾板',
			'state' => '状态',
			'create_time' => '创建时间',
		);
	}
	
	public function excelAttributes(){//导出excel所需的字段名、列号、宽度
		return array(
			'plate_no'=>array('B',18),
			'type'=>array('C',10),
			'property_owner'=>array('D',15),
			'kind'=>array('E',15,'getKindName'),
			'premium_name'=>array('F',15),
			'insurer'=>array('G',15),
			'policyno'=>array('H',15),
			'total_cost'=>array('I',15),
			'effectdate'=>array('J',15),
			'lapsedate'=>array('K',15),
		);
	}
	
	public function getKindNames(){
		return $this->_kind;
	}
	
	public function getKindName($n=null){
		return $this->_kind[$n==null?$this->kind:$n];
	}
	
	public function safeAttributes(){
		$params=parent::safeAttributes();
		$params[]='property_owner';
		$params[]='plate_no';
		$params[]='type';
		return $params;
	}
}